package com.p2p.terminal.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.p2p.common.enums.ResponseEnum;
import com.p2p.common.utils.ParamUtils;
import com.p2p.common.utils.ReflectionUtils;
import com.p2p.common.utils.ResMessage;
import com.p2p.common.utils.StringUtils;
import com.p2p.common.utils.Underline2Camel;
import com.p2p.terminal.dao.IpDao;
import com.p2p.terminal.dao.TerminalDataDao;
import com.p2p.terminal.po.P2pIp;
import com.p2p.terminal.vo.IpCondition;
import com.p2p.terminal.vo.TerminalData;
import com.p2p.terminal.vo.TerminalDataCondition;

@Repository
public class TerminalDataDaoImpl implements TerminalDataDao {

	private static final Logger logger = LoggerFactory.getLogger(TerminalDataDaoImpl.class);

	@Resource
	private JdbcTemplate jdbcTemplate;

	@Override
	public ResMessage<TerminalData> selectIpData(TerminalDataCondition dataCondition) {
		Long agentId = dataCondition.getAgentId();
		Integer agentLevel = dataCondition.getAgentLevel();
		String cmac = dataCondition.getCmac();
		String mac = dataCondition.getMac();
		String version = dataCondition.getVersion();
		String ip = dataCondition.getIp();
		String startTime = dataCondition.getStartTime();
		String endTime = dataCondition.getEndTime();

		int timeDiff = startTime.compareTo(endTime);

		if (timeDiff >= 1) {
			startTime = startTime + endTime;
			endTime = startTime.substring(0, startTime.length() - endTime.length());
			startTime = startTime.substring(endTime.length());
		}


		Integer pageIndex = dataCondition.getPageIndex();
		Integer pageSize = 20;
		String sortLineName = dataCondition.getSortLineName();
		String orderType = dataCondition.getOrderType();

		String sqlName = Underline2Camel.camel2Underline(sortLineName, true);

		String table1 = "p2p_start_" + startTime.replaceAll("(\\d{4})-(\\d{2}).+", "$1$2");
		String table2 = "p2p_start_" + endTime.replaceAll("(\\d{4})-(\\d{2}).+", "$1$2");

		RowMapper<TerminalData> rowMapper = new BeanPropertyRowMapper<>(TerminalData.class);

		String cmacSql = "";
		if ("0".equals(cmac) || StringUtils.isEmpty(cmac)) {
			cmacSql = "  1=1";

		} else {
			cmacSql = "  cmac = " + cmac;
		}
		String macSql = "";
		if ("0".equals(mac) || StringUtils.isEmpty(mac)) {
			macSql = " AND 1=1";

		} else {
			macSql = " AND mac = " + mac;
		}
		String versionSql = "";
		if ("0".equals(version) || StringUtils.isEmpty(version)) {
			versionSql = " AND  1=1";

		} else {
			versionSql = " AND version = " + version;
		}

		String ipSql = "";
		if ("0".equals(ip) || StringUtils.isEmpty(ip)) {
			ipSql = " AND  1=1";

		} else {
			ipSql = " AND ip = " + ip;
		}


		String agentSql = "";
		if (0 == agentId) {
			agentSql = " AND 1=1 ";

		} else {
			agentSql = "  AND agentid ="+agentId;
		}

		String orderSql = "";
		if (StringUtils.isEmpty(sortLineName)) {
			orderSql = " ";

		} else {
			orderSql = "  order by " + sqlName + " " + orderType;
		}

		int index = pageIndex == 1 ? 0 : ((pageIndex - 1) * pageSize);

		List<TerminalData> result = null;
		Integer pageCount = 0;
		if (table1.equals(table2)) {
			String countSql = "SELECT   count(1)  FROM " + table1 + "   WHERE " + cmacSql + macSql + ipSql + versionSql
					+ agentSql + " and date_time BETWEEN ? AND ? ;";
			String dataSql = "SELECT   *  FROM " + table1 + "   WHERE " + cmacSql  + macSql + ipSql
					+ versionSql + agentSql + " and date_time BETWEEN ? AND ?  " + orderSql + " limit ?,?";
			pageCount = jdbcTemplate.queryForObject(countSql, Integer.class, new Object[] { startTime, endTime });
			result = jdbcTemplate.query(dataSql, rowMapper, startTime, endTime, index, pageSize);
			System.out.println(dataSql);
		} else {
			String countSqlTable1 = "SELECT   count(1)  FROM " + table1 + "   WHERE " + cmacSql  + macSql
					+ ipSql + versionSql + agentSql + " and date_time >= ?  ";
			String countSqlTable2 = "SELECT   count(1)  FROM " + table2 + "   WHERE " + cmacSql  + macSql
					+ ipSql + versionSql + agentSql + " and date_time <= ? ";

			int pageCountTable1 = jdbcTemplate.queryForObject(countSqlTable1, Integer.class,
					new Object[] { startTime });
			int pageCountTable2 = jdbcTemplate.queryForObject(countSqlTable2, Integer.class, new Object[] { endTime });
			pageCount = pageCountTable1 + pageCountTable2;

			String dataSqlTable1 = "SELECT   *  FROM " + table1 + "   WHERE " + cmacSql + macSql + ipSql
					+ versionSql + agentSql + " and date_time >= ? " + orderSql;

			String dataSqlTable2 = "SELECT   *  FROM " + table2 + "   WHERE " + cmacSql + macSql + ipSql
					+ versionSql + agentSql + " and date_time <= ? " + orderSql;
			System.out.println(dataSqlTable1);
			List<TerminalData> resultTable1 = jdbcTemplate.query(dataSqlTable1, rowMapper, startTime);
			List<TerminalData> resultTable2 = jdbcTemplate.query(dataSqlTable2, rowMapper, endTime);

			if (StringUtils.isEmpty(sortLineName)) {
				resultTable1.addAll(resultTable2);
				result = resultTable1;
			} else {

			}

		}

		return new ResMessage<>(result, ResponseEnum.SUCCESS.getMsg(), true, pageCount);
	}

	private String list2String(List list, String division) {
		StringBuilder itmeString = new StringBuilder();
		list.forEach(item -> {
			itmeString.append(item + division);
		});
		itmeString.deleteCharAt(itmeString.length() - 1);
		return itmeString.toString();
	}

}
